/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import BUSObject.CartItem;
import BUSObject.Product;
import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author HOANG ANH
 */
public class BUSCart {

    private HashMap<Integer, CartItem> items = new HashMap<Integer, CartItem>();

    public void AddProduct(Product p) {
        if (items.containsKey(p.getID())) {
            CartItem cartItem = items.get(p.getID());
            items.get(p.getID()).setQuantity(cartItem.getQuantity() + 1);
        } else {
            CartItem cartItem = new CartItem(p, 1);
            items.put(p.getID(), cartItem);
        }
    }

    public void removeProduct(int key) {
        if (items.containsKey(key)) {
            items.remove(key);
        }
    }

    public void editQuantity(int key, int quantity) {
        items.get(key).setQuantity(quantity);
    }

    public Iterator<CartItem> getList() {
        return items.values().iterator();
    }

    public float getCost() {
        float TotalCost = 0;
        for (Iterator<CartItem> it = items.values().iterator(); it.hasNext();) {
            CartItem cartItem = it.next();
            
            TotalCost += cartItem.getPrice() * cartItem.getQuantity();
        }
        return TotalCost;
    }
    
    public void clear(){
        items.clear();
    }
}
